<?php
namespace app\api\model;


use app\model\Base;

class UserDiscount extends Base
{
    //钱包增加逻辑 induce deduce locking unlocking
    //userId,type,mode,source,objectId,amount,fee_amount,remark
    //userId,coin,point,locking_point,balance
    public static function induce($userId, $amount, $wallet_type = 14, $wallet_des = 14, $alias = 1, $des = '')
    {
        /**
         * TODO
         * $type 判断是否需要手续费
         * 手续费比例后台配置，直接获取后台手续费比例
         * 加手续费字段，扣除手续费逻辑
         */
        $item = self::find($userId);
        $merchantName = 'merchant' . $alias;
        $add = [];
        $add['user_id'] = $userId;
        $add['merchant_id'] = $alias;
        $add['amount'] = $amount;
        $add['type'] = Base::$walletTypeArray[$wallet_type];
        $add['content'] = $des == '' ? Base::$walletLogDesArray[$wallet_des] : $des;

        $add['before'] = $item[$merchantName];

        $update = [];
        $update[$merchantName] = $add['before'] + $amount;

        $item->save($update);
        UserDiscountLog::create($add);
        return true;
    }

    //钱包减少逻辑 induce deduce locking unlocking
    //userId,type,mode,source,objectId,amount,fee_amount,remark
    //userId,coin,point,locking_point,balance
    public static function deduce($userId, $amount, $wallet_type = 6, $wallet_des = 6, $alias = 1, $des = '')
    {
        /**
         * TODO
         * $type 判断是否需要手续费
         * 手续费比例后台配置，直接获取后台手续费比例
         * 加手续费字段，扣除手续费逻辑
         */
        $item = self::find($userId);
        $merchantName = 'merchant' . $alias;
        $add = [];
        $add['user_id'] = $userId;
        $add['merchant_id'] = $alias;
        $add['amount'] = -$amount;
        $add['type'] = Base::$walletTypeArray[$wallet_type];
        $add['content'] = $des == '' ? Base::$walletLogDesArray[$wallet_des] : $des;

        $add['before'] = $item[$merchantName];

        $update = [];
        $update[$merchantName] = $add['before'] - $amount;

        $item->save($update);
        UserDiscountLog::create($add);
        return true;
    }
}